home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6060 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  58 lines

  1. Path: calvin.risq.qc.ca!news
  2. From: pcoulomb@criq.qc.ca (Pierre Coulombe)
  3. Newsgroups: comp.lang.c
  4. Subject: Type casting
  5. Date: 21 Feb 1996 18:18:58 GMT
  6. Organization: CRIQ
  7. Message-ID: <4gfnmi$gsc@calvin.risq.qc.ca>
  8. NNTP-Posting-Host: pcoulomb.criq.qc.ca
  9. Mime-Version: 1.0
  10. Content-Type: Text/Plain; charset=US-ASCII
  11. X-Newsreader: WinVN 0.99.6
  12.  
  13. I have a problem with type casting in Visual C 1.5.
  14. I expected the following program to print the value 254 for valI.
  15. Instead it gives the output shown below.
  16. Can someone tell me where is my mistake ?
  17.  
  18.  
  19. ******** Program *********
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24.  
  25. typedef unsigned int UINT;
  26.  
  27. #define MM_TO_UNITS 10.0F
  28.  
  29. void main(void)
  30. {
  31.   float valF;
  32.   UINT  valI;
  33.   char string[80];
  34.  
  35.   strcpy(string, "25.4");
  36.  
  37.   valI =  (UINT) ((float) atof(string) * MM_TO_UNITS);
  38.   printf("valI = %u\n", valI);
  39.  
  40.   sprintf(string, "%f", (float) atof(string) * MM_TO_UNITS);
  41.   printf("string = %s\n", string);
  42.  
  43.   strcpy(string, "25.4");
  44.   sprintf(string, "%u", (UINT) ((float) atof(string) * MM_TO_UNITS));
  45.   printf("string = %s\n", string);
  46. }
  47.  
  48.  
  49. ******** Output *********
  50.  
  51. valI = 253
  52. string = 254.000000
  53. string = 253
  54.  
  55.  
  56.  
  57.  
  58.